/**

 @author 石头哥哥
 </P>
 Date:   2014/12/19
 </P>
 Time:   17:26

 注解：

 */
package {
import GameObject.Bomb;
import GameObject.Bullet;
import GameObject.Enemy;

import starling.core.Starling;
import starling.display.MovieClip;
import starling.events.Event;

import utils.ArrayList;
import utils.VectorList;

public class GameManger {


    /**
     *
     */
    private static var instance:GameManger;
    private static var inited:Boolean = false;

    /**
     * 敌机子弹集合类
     */
    public static var enemyBulletList:Vector.<Bullet> = new Vector.<Bullet>();

    /**
     * hero子弹集合类
     */
    public static var heroBulletList:Vector.<Bullet> = new Vector.<Bullet>();

    /**
     * 敌机对象集合
     */
    public static var enemyList:Vector.<Enemy> = new Vector.<Enemy>();

    /**游戏中的bomb对象集合*/
    public static var bombList:VectorList = new VectorList();


    /**
     * 游戏对象管理类
     */
    public function GameManger() {
        if (inited)throw new Error("EnemyManager is singleton ,don't create anymore");
    }

    /**
     * 获取单例对象
     * @return
     */
    public static function getInstance():GameManger {
        if (!inited) {
            instance = new GameManger();
            inited = true;
        }
        return instance;
    }

    /***
     *使用 bonb效果
     * remove  enemy obj and play effect
     */
    public function bonbClear():void {
        //enemy bullet
        for each(var bullet:Bullet in enemyBulletList) {
            bullet.clear(true);
        }
        enemyBulletList.splice(0, enemyBulletList.length);

        //enemy
        for each(var enemy:Enemy in enemyList) {
            //add score of hero
            _score += enemy.score;
            //clear view
            enemy.clear(true);
            //play effect   and sound
            var mc:MovieClip = enemy.downAnimateByType();
            mc.x = enemy.x;
            mc.y = enemy.y;
            GameSystem.root.addChild(mc);
            Starling.juggler.add(mc);
            mc.addEventListener(Event.COMPLETE,
                    function (e:Event):void {
                        Starling.juggler.remove(e.currentTarget as MovieClip);
                        GameSystem.root.removeChild(e.currentTarget as MovieClip);
                    });
        }
        enemyList.splice(0, enemyList.length);
    }

    /**
     * 清理缓存资源
     */
    public function clear():void {

        //bullet
        for each(var bullet:Bullet in enemyBulletList) {
            bullet.clear(true);
        }
        enemyBulletList.splice(0, enemyBulletList.length);


        //hero  bullet
        for each(var heroBullet:Bullet in heroBulletList) {
            heroBullet.clear(true);
        }
        heroBulletList.splice(0, heroBulletList.length);


        //enemy
        for each(var enemy:Enemy in enemyList) {
            enemy.clear(true);
        }
        enemyList.splice(0, enemyList.length);

        //bone
        for each(var bone:Bomb in bombList) {
            bone.clear(true);
        }
        bombList.clear();

        // reset score
        _score = 0;
        rewardBone = 10;

    }


    /**
     * hero
     */
    private var _score:int = 0;
    private var rewardBone:int = 20;
    public static var fireType:int = 1;// hero fire type  1,2,3


    /**
     * create bone
     * @return
     */
    public function createBone():Bomb {
        if (_score >= rewardBone) {
            var bone:Bomb = new Bomb();
            rewardBone = rewardBone * 2;// double
            return bone;
        }
        return null;
    }

    public function get score():int {
        return _score;
    }

    public function set score(value:int):void {
        _score = value;
    }
}
}
